﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Products;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.ProductTyped
{
    public class TypeListHandler : IRequestHandler<TypeListCommon, ResultsDTO<TypeDto>>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBateRepository<ProductTypes> ProductTypesRepository;
        private readonly ILogger<TypeListHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productTypesRepository"></param>
        /// <param name="logger"></param>
        public TypeListHandler(IBateRepository<ProductTypes> productTypesRepository, ILogger<TypeListHandler> logger)
        {
            ProductTypesRepository = productTypesRepository;
            this.logger = logger;
        }
        #endregion

        #region 树形 产品分类
        /// <summary>
        /// 树形 产品分类
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO<TypeDto>> Handle(TypeListCommon request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper dapperHelper = new DapperHelper();
                //ResultsDTO<TypeDto>对象result，并设置Code为 0
                ResultsDTO<TypeDto> result = new ResultsDTO<TypeDto>() { Code = 0 };
                //连接mySql
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";
                //创建新的实例 pro
                ProductTypes pro = new ProductTypes();

                //sql语句
                string str = $"select * from producttypes where 1=1";

                var list = ProductTypesRepository.GetAll(str, pro, conn).ToList();

                List<TypeDto> dto = fangFa(list, 0);

                result.Data = dto; //将dto列表赋值给result对象的Data属性 可以存储查询产品类型数据
                result.Message = "物料产品分类 显示列表成功";

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 树形
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="productTypeName"></param>
        /// <returns></returns>
        public List<TypeDto> fangFa(List<ProductTypes> allTypes, int pid)
        {
            ProductTypes productTypes = new ProductTypes();

            List<TypeDto> dto = new List<TypeDto>();
            //遍历allTypes列表中Pid等于传入的pid的每个元素item
            //使用linq
            foreach (var item in allTypes.Where(x=>x.Pid==pid))
            {
                TypeDto node = new TypeDto
                {
                    ID = item.ProductTypeId,
                    Title = item.ProductTypeName,
                    Pid = item.Pid,
                    Children = fangFa(allTypes, item.ProductTypeId)
                };
                dto.Add(node);
            }
            return dto;
        }
        #endregion

    }
}
